package com.wsg.lightaccounting.Model;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.wsg.lightaccounting.Activity.MainActivity;
import com.wsg.lightaccounting.DB.DB;
import com.wsg.lightaccounting.R;

import java.io.IOException;
import java.util.List;

/**
 * Created by dell-pc on 2016/11/10.
 */

public class AccountManager {
    private static AccountManager accountManager;
    private static DB db;
    private Context context;

    public static List<Account> accounts;
    public static List<CashCard> cashCards;
    public static List<CreditCard> creditCards;
    public static List<Customized> customizeds;
    public static List<NetworkPayment> networkPayments;

    private AccountManager(Context context){
        try {
            db = DB.getInstance(context);
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.context = context;
    }

    public static AccountManager getAccountManager(Context context){
        if (accountManager==null) accountManager = new AccountManager(context);
        return accountManager;
    }

    public static float getSum(){
        float sum = 0f;
        for (int i = 0; i <accounts.size() ; i++) {
            sum =sum + accounts.get(i).getBalance();
        }
        return sum;
    }

    public void deleteAccount(Account account){
        for (int i = 0; i < RecordManager.expenditures.size() ; i++) {
            if (RecordManager.expenditures.get(i).getMoneytype() == account.getId()){
            RecordManager.getRecordManager(context).deleteRecord(RecordManager.expenditures.get(i));}
        }
        for (int i = 0; i < RecordManager.incomes.size(); i++) {
            if (RecordManager.incomes.get(i).getMoneytype() == account.getId()){
                RecordManager.getRecordManager(context).deleteRecord(RecordManager.incomes.get(i));}
        }
        switch (account.getType()){
            case 1:
                    db.deleteNetworkPayment(findNetworkPayment(account.getId()).getId());
                    networkPayments.remove(findNetworkPayment(account.getId()));
                    break;
            case 2:
                    db.deleteCashCard(findCashCardById(account.getId()).getId());
                    cashCards.remove(findCashCardById(account.getId()));
                    break;
            case 3:
                    db.deleteCreditCard(findCreditCardById(account.getId()).getId());
                    creditCards.remove(findCreditCardById(account.getId()));
                    break;
            case 4:
                    db.deleteCustomized(findCustomized(account.getId()).getId());
                    customizeds.remove(findCustomized(account.getId()));
                    break;
                default:
                    break;
        }
        db.deleteAccount(account.getId());
        accounts.remove(account);
    }
    public  void getAllAccount(){
        db.getAllAccount();
    }

    public static void setAccountColor(Context context,View view,int i){
        String color = "color" + (i) ;
        int resID = context.getResources().getIdentifier(color, "color", "com.wsg.lightaccounting");
        view.setBackgroundResource(resID);
    }
    public static void setAccountInformation(TextView name,TextView information,Account account){
        switch (account.getType()){
            case 0:
                name.setText("现金");
                information.setText("");
                break;
            case 1:
                name.setText(findNetworkPayment(account.getId()).getName());
                information.setText(findNetworkPayment(account.getId()).getInformation());
                break;
            case 2:
                name.setText(findCashCardById(account.getId()).getName());
                information.setText(findCashCardById(account.getId()).getCard_id());
                break;
            case 3:
                if (findCreditCardById(account.getId())!= null)
                name.setText(findCreditCardById(account.getId()).getName());
                information.setText(findCreditCardById(account.getId()).getCard_id());
                break;
            case 4:
                name.setText(findCustomized(account.getId()).getName());
                information.setText("");
                break;
            default:
                break;
        }
    }
    public static void setAccountName(TextView name,Account account){
        switch (account.getType()){
            case 0:
                name.setText("现金");
                break;
            case 1:
                name.setText(findNetworkPayment(account.getId()).getName());
                break;
            case 2:
                name.setText(findCashCardById(account.getId()).getName());
                break;
            case 3:
                name.setText(findCreditCardById(account.getId()).getName());
                break;
            case 4:
                name.setText(findCustomized(account.getId()).getName());
                break;
            default:
                break;
        }
    }
    public static void setAccountIcon(View view,int i){
        switch (i){
            case 0:
                view.setBackgroundResource(R.mipmap.cash);
                break;
            case 1:
                view.setBackgroundResource(R.mipmap.e_payment);
                break;
            case 2:
                view.setBackgroundResource(R.mipmap.diposit_card);
                break;
            case 3:
                view.setBackgroundResource(R.mipmap.credit_card);
                break;
            case 4:
                view.setBackgroundResource(R.mipmap.e_credit);
                break;
            default:
                break;
        }
    }

    public static CashCard findCashCardById(long id){
        CashCard cashCard = null ;
        for (int i =0;i<cashCards.size();i++)
        {
            if (id == cashCards.get(i).getAccount_id())
            {
                cashCard = cashCards.get(i);
                break;
            }
        }

        return cashCard;
    }

    public static CreditCard findCreditCardById(long id ){
        CreditCard creditCard = null;
        for (int i =0;i<creditCards.size();i++)
        {
            if (id == creditCards.get(i).getAccount_id())
            {
               creditCard = creditCards.get(i);
                break;
            }
        }
        return creditCard;
    }

    public static NetworkPayment findNetworkPayment(long id ){
        NetworkPayment networkPayment = null;
        for (int i =0;i<networkPayments.size();i++)
        {
            if (id == networkPayments.get(i).getAccount_id())
            {
               networkPayment = networkPayments.get(i);
                break;
            }
        }
        return networkPayment;
    }

    public static Customized findCustomized(long id){
        Customized customized = null;
        for (int i =0;i<customizeds.size();i++)
        {
            if (id == customizeds.get(i).getAccount_id())
            {
                customized = customizeds.get(i);
                break;
            }
        }
        return customized;
    }

    public void addCashCard(CashCard cashCard){
        Account account = new Account(0,0,2,2);
        long id = db.insertAccount(account);
        cashCard.setAccount_id(id);
        db.insertCashCard(cashCard);
        cashCards.add(cashCard);
        accounts.add(account);
    }

    public void addCreditCard(CreditCard creditCard){
        Account account = new Account(0,0,3,3);
        long id = db.insertAccount(account);
        creditCard.setAccount_id(id);
        if(db.insertCreditCard(creditCard)!=-1)
            creditCards.add(creditCard);
        accounts.add(account);
    }
    public void addNetworkPayment(NetworkPayment networkPayment){
        Account account = new Account(0,0,1,1);
        long id = db.insertAccount(account);
        networkPayment.setAccount_id(id);
        db.insertNetworkPayment(networkPayment);
        networkPayments.add(networkPayment);
        accounts.add(account);
    }

    public void addCustomezed(Customized customized,float balance,int color){
        Account account = new Account(0,balance,4,color);
        long id = db.insertAccount(account);
        customized.setAccount_id(id);
        db.insertCustomized(customized);
        customizeds.add(customized);
        accounts.add(account);
    }

    public void updateCashCard(long id,String bank,String card_id,String name){
        CashCard cashCard = findCashCardById(id);
        cashCard.setName(name);
        cashCard.setBank(bank);
        cashCard.setCard_id(card_id);
        db.updateCashCard(cashCard);
    }

    public void updateCreditCard(long id,String bank,String card_id,String name,float limit){
        CreditCard creditCard = findCreditCardById(id);
        creditCard.setName(name);
        creditCard.setBank(bank);
        creditCard.setLimit(limit);
        creditCard.setCard_id(card_id);
        db.updateCreditCard(creditCard);
    }
    public void updateNetworkPayment(long id,String name,String information){
        NetworkPayment networkPayment = findNetworkPayment(id);
        networkPayment.setName(name);
        networkPayment.setInformation(information);
        db.updateNetworkPayment(networkPayment);
    }
    public static Account findAccountById(long id){
        Account account =null;
        for (int i = 0; i < accounts.size(); i++) {
            if (id == accounts.get(i).getId()){
                account = accounts.get(i);
                break;
            }
        }
        return account;
    }

    public void updateCustomzed(long id,String name,float balance,int color){
        Account account = findAccountById(id);
        Customized customized = findCustomized(id);
        account.setBalance(balance);
        account.setColor(color);
        customized.setName(name);
        db.updateAccount(account);
        db.updateCustomized(customized);
    }
    public void updateCashAccount(float balance){
        Account account = findAccountById(0L);
        account.setBalance(balance);
        db.updateAccount(account);
    }
}
